package codeflyer.server.utils;

import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.apache.log4j.Logger;

import codeflyer.server.Main;
import codeflyer.server.Session;

/**
 * @author CodeFlyer
 * @date May 10, 2020
 * @version 1.0
 */

public class CountDownUtil {
	
	private Map<String, Session> sessions = null;
	private static Logger logger = Logger.getLogger(CountDownUtil.class);
	private static ExecutorService service = Executors.newCachedThreadPool();
	private Map<String, Long> timeMap = null;
	private String ip = null;

	public CountDownUtil(Map<String, Session> sessions,Map<String, Long> timeMap,String ip) {
		super();
		this.timeMap = timeMap;
		this.ip = ip;
		this.sessions = sessions;
	}
	
	public void executorCountDown() {
		service.execute(new Task());
	}
	
	private class Task implements Runnable {
		@Override
		public void run() {
			// TODO Auto-generated method stub
			int t = 0;
			while(true) {
				Long start = timeMap.get(ip);
				try {
					Thread.sleep(60000);    //1分钟
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				t++;
				if(t>=2) {
					Long time = System.currentTimeMillis() - start;
					if((time/1000.0)>=117.0) {
						break;
					}
				}
			}
			timeMap.remove(ip);
			sessions.remove(ip);  //移除原有的session
			System.out.println("已超时");
			logger.info("一个session超时");
		}
	}

}


